﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Data.Sql;
using System.Web.UI;
using sonicERPinfo;
using sonicERPDAL;


namespace sonicERPinterface.Charp
{
    public class Util:System.Web.UI.Page


    {





        public void gerarxml(DataTable dt, string caminhoxml, string nometabela)
        {
            try
            {
                foreach (DataColumn item in dt.Columns)
                {

                    item.ColumnName = item.ColumnName.TrimEnd();
                    item.ColumnName = item.ColumnName.TrimStart();
                    item.ColumnName = item.ColumnName.Trim();
                    item.ColumnName = item.ColumnName.Replace(" ", "_");
                }

              
             XmlTextWriter xml = new XmlTextWriter(Server.MapPath(caminhoxml), Encoding.UTF8);
             xml.WriteStartDocument();
             xml.WriteStartElement(nometabela );
                 foreach (DataRow item in dt.Rows)
                {
                     xml.WriteStartElement(nometabela);
                    for (int y = 0; y < dt.Columns.Count; y++)
                    {
                        xml.Formatting = Formatting.Indented;
                        xml.WriteElementString(dt.Columns[y].ColumnName, item[y].ToString());
                    }
                    xml.WriteEndElement();
                }
               xml.WriteFullEndElement();
               xml.WriteEndDocument();
               xml.Close();
                      

            }
            catch (Exception ex )
            {

                throw new Exception(ex.Message);
            }

        }
 


           



        public System.Data.DataTable getxml(string caminhoxml)
        {


            System.Data.DataTable dt = new System.Data.DataTable();
            DataSet ds = new DataSet("XMLDataSet");
            if (System.IO.File.Exists(caminhoxml))
            {


                XmlDocument xml = new XmlDocument();
                xml.Load(caminhoxml);
                ds.ReadXml(caminhoxml);

            }
            else
            {
                throw new Exception("Arquivo XML não Existe");
            }
            dt = ds.Tables[0];
            return (dt);
        }
    }
}